<script>
// super hacky ajax
function getEntities(id){
  //get the list of tasks and rewrite the task
  $.getJSON(location.origin + "/<%=@project_name%>/results/"+ id +".json", function(data) {
    //console.log(data);
    $( "#entities" ).empty();
    // Set the values based on just this task's allowed type
    var entity_string="<ul>";

    $.each(data["entities"], function(id, hash) {
      type = hash["type"].split(':')[4];
      name = hash["name"];
      entity_string += "<li><a href='/<%=@project_name%>/entities/" + hash["id"] + "'> " + type + ": " + name + "</a> ";
      if ("uri" in hash["details"]){
        entity_string += ("<a target='_blank' href='" + hash["details"]["uri"] + "'>[link]</a>");
      };
      entity_string += "</li>";
    });
    entity_string += "</ul>";
    //console.log(entity_string);
    $( "#entities" ).append(entity_string);
  });
}

window.setInterval(function(){
  getEntities(<%=@result.id%>);  //calling every 2 seconds
}, 2000);

//call right away
getEntities(<%=@result.id%>);
</script>
<div class="row">
<div class="col-sm-5">
  <h3>Task Result: <a href="<%=@rerun_uri || nil%>"><%=@result.task_name %></a> </h3>
  <p>ID: <%=@result.id %> </p>
  <p>Start: <%=@result.timestamp_start %> </p>
  <p>End: <%= @result.timestamp_end %> </p>
  <p>Elapsed (s): <%= @elapsed_time %> </p>
  <p>Job ID: <%= @result.job_id %> </p>
  <p>Handlers: <%= @result.handlers %></p>
  <p>Depth: <%=@result.depth %> </p>
  <p>Entity: <a href="/<%=@project_name%>/entities/<%=@result.base_entity.id if @result.base_entity %>"><%= h "#{@result.base_entity}" %></a></p>
  <p>Cancelled: <%= @result.cancelled %> (<a href="<%=request.path%>/cancel">Cancel</a>)</p>
  <p>Complete: <%= @result.complete %></p>
  <p>Entity Count: <%= @result.entities.count %></p>
  <p>Strategy: <%= @result.scan_result.strategy if @result.scan_result %></p>
  <p>Max Depth: <%=@result.scan_result.depth if @result.scan_result %> </p>
  <p>Whitelist Strings: <%= @result.scan_result.whitelist_strings if @result.scan_result %></p>

  Export:
  <ul>
    <li><a href=<%="#{request.url}.json"%>> JSON </a></li>
  </ul>
  Entities:
  <div id="entities">
    <p>[Gathering entities...]</p>
  </div>
</div>
<div class="col-sm-5">
<%= erb :_log, :layout => false %>
</div>
</div>
